//
// Created by Administrator on 2024/10/21.
//

#include "HttpServer.h"


HttpServer::HttpServer() {
    m_port = CIVET_SERVER_PORT;
    mg_init_library(0);
    std::vector<std::string> options = {
            "document_root", ".",
            "num_threads", "1",
            "listening_ports", m_port
    };
    m_server = std::make_unique<CivetServer>(options);
    m_handlers = HandlerRegistrar::registerHandlers();
}

HttpServer::~HttpServer() {
    m_server->close();
}

void HttpServer::run() {
    for (auto &pair: m_handlers) {
        m_server->addHandler(pair.first, pair.second);
    }
    while (true) {
        sleep(1);
    }
    mg_exit_library();
}

std::string HttpServer::getPort() const {
    return m_port;
}
